package com.etime.shycourse.dao;

import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.baomidou.mybatisplus.core.toolkit.Constants;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.etime.shycourse.dto.AdLogDto;
import com.etime.shycourse.dto.CourseOfflineDto;
import com.etime.shycourse.pojo.AdLog;
import com.etime.shycourse.pojo.Course;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;
import org.springframework.stereotype.Repository;

import java.util.Date;
import java.util.List;


/**
 * @author why
 * @data 2022/7/18 21:29
 */
@Repository
public interface AdLogMapper extends BaseMapper<AdLog> {
    /**
     * 根据广告位置查询广告列表
     * 实现分页，数据映射到AdLogDto中
     * @param page
     * @param wrapper
     * @return
     */
    @Select("select nt.* from(" +
            "SELECT " +
            " adlog.*,  " +
            " course.coursetitle,  " +
            " adplace.placename " +
            "FROM " +
            " adlog " +
            " LEFT JOIN " +
            " adplace " +
            " ON  " +
            "  adlog.placeid = adplace.placeid " +
            " LEFT JOIN " +
            " course " +
            " ON  " +
            " adlog.adcourseid = course.courseid) as nt  ${ew.customSqlSegment}")
    Page<AdLogDto> selectAdLogPage(Page<AdLogDto> page, @Param(Constants.WRAPPER) QueryWrapper<AdLogDto> wrapper);

    /**
     * 根据广告位限定的课程类型查询匹配的课程
     * @param placeCourseTypeId
     * @return
     */
    @Select("<script>" +
            "SELECT " +
            " course.courseid,  " +
            " course.coursetypeid,  " +
            " course.coursetitle,  " +
            " course.coursesubtitle,  " +
            " course.coursestate,  " +
            " course.courseaudit,  " +
            " course.courseauditdesc,  " +
            " course.coursedesc,  " +
            " course.coursegoals,  " +
            " course.courseaudiences,  " +
            " course.coursepic,  " +
            " course.coursecreatetime,  " +
            " course.courseupdatetime,  " +
            " course.courseprice,  " +
            " course.courseoriginprice,  " +
            " course.coursecoinprice,  " +
            " course.coursecoinmaxpoint,  " +
            " course.coursecoinused,  " +
            " course.coursescore,  " +
            " course.coursepullcount,  " +
            " course.courserecommended,  " +
            " course.coursesort,  " +
            " course.coursecommentcount,  " +
            " course.coursepaytype,  " +
            " course.courseteacherid,  " +
            " course.courseteachername,  " +
            " course.coursesalerpoint,  " +
            " course.coursetecpoint,  " +
            " course.coursetecorg,  " +
            " course.coursevaliddays,  " +
            " course.courseorgid,  " +
            " course.coursetype,  " +
            " course.courseadvice,  " +
            " course.classbegin,  " +
            " course.classlessonnum,  " +
            " course.classorg,  " +
            " course.classteacherinfo,  " +
            " course.field1,  " +
            " course.field2,  " +
            " course.field3,  " +
            " course.field4,  " +
            " course.field5,  " +
            " course.field6 " +
            "FROM " +
            " course " +
            "<where> " +
            "<if test='placecoursetypeid!=null&amp;&amp;placecoursetypeid!=-1'> " +
            "and coursetypeid = #{placecoursetypeid} " +
            "or coursetypeid IN ((SELECT coursetypeid FROM coursetype " +
            "WHERE courseptypeid = #{placecoursetypeid})) " +
            "</if> " +
            "<if test='coursetitle!=null'> " +
            "and coursetitle like '%${coursetitle}%' " +
            "</if> " +
            "</where> " +
            "</script>")
    Page<Course> selectCourseByPlaceCourseTypeId(Page<Course> page, @Param("placecoursetypeid") Long placeCourseTypeId, @Param("coursetitle") String coursetitle);

    /**
     * 根据广告位分页查询该广告位在此时的所有可用的广告课程
     * @param page
     * @param placetype
     * @param nowTime
     * @return
     */
    @Select("SELECT course.*,b.* FROM adplace RIGHT JOIN adlog ON  " +
            " adplace.placeid = adlog.placeid inner JOIN course " +
            " ON adlog.adcourseid = course.courseid " +
            " inner join offlineshowattrconfig b on course.courseid = b.courseid " +
            " WHERE adplace.placetype = #{placetype} AND adlog.adstate = 1 AND  " +
            " adlog.adstart <= #{nowTime} AND adlog.adend >= #{nowTime} " +
            " ORDER BY adlog.adsort ASC")
    Page<CourseOfflineDto> selectCourseByPlaceType(Page<CourseOfflineDto> page, @Param("placetype") Long placetype, @Param("nowTime") String nowTime);

    @Select("SELECT " +
            " adlog.adid,  " +
            " adlog.placeid,  " +
            " adlog.adimg,  " +
            " adlog.adcourseid,  " +
            " adlog.adlink,  " +
            " adlog.adstart,  " +
            " adlog.adend,  " +
            " adlog.adprice,  " +
            " adlog.adsumprice,  " +
            " adlog.adsort,  " +
            " adlog.adstate " +
            "FROM " +
            " adlog " +
            "WHERE " +
            " adlog.adstate IN(1,2)  " +
            " AND " +
            " adlog.placeid = #{placeid}")
    List<AdLog> statisticsAdLogByPlaceId(@Param("placeid") Long placeId);

    /**
     * 根据当前时间修改广告状态
     * @param nowDate
     * @return
     */
    @Update("UPDATE adlog SET adlog.adstate=1 WHERE adlog.adstart < #{nowDate} AND adlog.adend > #{nowDate} AND adlog.adstate=2")
    Integer updateAdLogPut(@Param("nowDate") String nowDate);

}
